import { Strategy } from 'passport-local'
import { PassportStrategy } from '@nestjs/passport'
import { Injectable, UnauthorizedException } from '@nestjs/common'
import { AuthService } from './auth.service'
import { BusinessException } from 'src/common/exceptions/business.exception'

@Injectable()
export class LocalStrategy extends PassportStrategy(Strategy) {
  constructor(private readonly authService: AuthService) {
    super()
  }
  // 用户验证
  async validate(userName: string, password: string): Promise<any> {
    console.log('validate', userName, password)

    const user = await this.authService.validateUser(userName, password)
    if (!user) {
      throw new BusinessException('账号或密码错误')
    }
    return user
  }
}
